﻿using Business.Core.Annotations;
using Common;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManagerSys.backoffice.project
{
    /// <summary>
    /// 设置上班时间
    /// </summary>
    public class SetTimeMerber : Common.BusinessBase
    {
        [Doc("获取上班时间的json字符串", Group = "上班时间管理")]
        public virtual async Task<dynamic> GetWorkTimeJson()
        {
            string json = JsonFileHelp.ReadJsonFile(Environment.CurrentDirectory + "/appsettings.json", "worktime");

            return (json);
        }

        [Doc("修改上班时间的json字符创", Group = "上班时间管理")]
        public virtual async Task<dynamic> UpdateWorkTimeJson(string json)
        {
            worktimemodel oModel = JsonConvert.DeserializeObject<worktimemodel>(json);
            //计算一天的上班总时长
            TimeSpan ts_morning =Convert.ToDateTime(oModel.morning_end) - Convert.ToDateTime(oModel.morning_start);
            TimeSpan ts_afternoon = Convert.ToDateTime(oModel.afternoon_end) - Convert.ToDateTime(oModel.afternoon_start);
            oModel.total = ((ts_morning + ts_afternoon).TotalMinutes)/60;
            oModel.morningtotal = ts_morning.TotalMinutes / 60;
            oModel.afternoontotal = ts_afternoon.TotalMinutes / 60;
            oModel.spacetotal = (Convert.ToDateTime(oModel.afternoon_start) - Convert.ToDateTime(oModel.morning_end)).TotalMinutes / 60;
            //oModel.total = ts_morning.Hours + ts_afternoon.Hours + ts_morning.Minutes + ts_afternoon.Minutes;
            bool result = JsonFileHelp.UpdateJsonFile(Environment.CurrentDirectory + "/appsettings.json",JsonConvert.SerializeObject(oModel), "worktime");
            string message = (result ? "修改成功" : "修改失败，请稍后重试");
            return (new { message = message});
        }

        public class worktimemodel
        {
            public string morning_start { set; get; } //上午上班的开始时间
            public string morning_end { set; get; }//上午上班的结束时间
            public string afternoon_start { set; get; }//下午上班的开始时间
            public string afternoon_end { set; get; }//下午上班的结束时间
            public double total { get; set; }//一天上班的总时间
            public double morningtotal { get; set; }//上午上班的总时间
            public double afternoontotal { get; set; }//下午上班的总时间
            public double spacetotal { get; set; } //中午休息时长
        }
    }
}
